package com.matt.service.courseV2.impl;

import com.dotflat.util.AssertUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.courseV2.VendorCourse;
import com.matt.domain.courseV2.VendorCoursePackage;
import com.matt.domain.courseV2.vo.VendorCourseVO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.courseV2.VendorCourseContentRepository;
import com.matt.repository.courseV2.VendorCoursePackageRepository;
import com.matt.service.courseV2.VendorCoursePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class VendorCoursePackageServiceImpl implements VendorCoursePackageService {

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private VendorCoursePackageRepository vendorCoursePackageRepository;

    @Autowired
    private BaseNativeSqlRepository baseNativeSqlRepository;

    @Autowired
    private VendorCourseContentRepository vendorCourseContentRepository;

    @Override
    public VendorCoursePackage getOne(String id) {
        return vendorCoursePackageRepository.getOne(id);
    }

    public VendorCoursePackage getVendorCoursePackage(String vendorId){
        return null;
    }

    public VendorCoursePackage addVendorCoursePackage(String name, String desc, String contact, String logo, String cover){
        return null;
    }

    @Override
    public List<VendorCoursePackage> list(String areaId,int type, PagerQuery pagerQuery) {
        int start = pagerQuery.getPageNum() * pagerQuery.getPageSize();
        int size = pagerQuery.getPageSize();
        return vendorCoursePackageRepository.areaIdByCoursePackageList(areaId,type,"","0", start, size);
    }

    @Override
    public Page<VendorCoursePackage> list(String areaId, int type, String name,String parentId, PagerQuery pagerQuery) {
        if (Objects.isNull(name)){
            name = "";
        }
        int start = pagerQuery.getPageNum() * pagerQuery.getPageSize();
        int size = pagerQuery.getPageSize();
        List<VendorCoursePackage> list = vendorCoursePackageRepository.areaIdByCoursePackageList(areaId,type, name, parentId,start, size);

        Integer total = vendorCoursePackageRepository.areaIdByCoursePackageCount(areaId,type, name,parentId);
        return  new PageImpl<>(list, PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize()), total);
    }

    @Override
    public Integer count(String areaId, int type) {
        return  vendorCoursePackageRepository.areaIdByCoursePackageCount(areaId, type,"","0");
    }


    @Override
    public List<VendorCourse> courseList(String ticket) {
        StaffDTO staff = cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        List<VendorCoursePackage>  list = vendorCoursePackageRepository.areaIdByCoursePackageList(staff.getAreaId(),-1,"","0",0,99999);
        if (list.size()>0){
           String classesIds = "";
            for (int i = 0; i < list.size(); i++) {
               if (list.size()-1 ==i){
                   classesIds+=list.get(i).getId();
               }else {
                   classesIds+=list.get(i).getId()+",";
               }
            }
            String sql  = " SELECT id,   title, topic_id AS topicId,course_package_id AS coursePackageId, author, authorId,   attachments, " +
                    " tools, index_in_package indexInPackage, tags, dimensions, guideVideo, content, `comment`, `guideText`, `ref_books` AS refBooks,  " +
                    " `content_template_id` AS contentTemplateId FROM  tc_vendor_course where course_package_id in ('"+classesIds+"') and deleted = 0";
            List<VendorCourse> objectArrayList = baseNativeSqlRepository.sqlObjectList(sql, VendorCourseVO.class);
            return objectArrayList;
        }

        return new ArrayList<>();
    }

    @Override
    public VendorCoursePackage add(VendorCoursePackage vendorCoursePackage) {
        return vendorCoursePackageRepository.save(vendorCoursePackage);
    }

    @Override
    public List<VendorCoursePackage> findByParentId(String parentId) {
        return vendorCoursePackageRepository.findByParentIdOrderByCreatedAsc(parentId);
    }

    @Override
    public List<VendorCoursePackage> findByParentIdOrId(String parentId) {
        return vendorCoursePackageRepository.findByParentIdOrId(parentId,parentId);
    }
}
